<?php
include_once("../configure/configure.php");		//--> global var
include_once("../lib/utils/AlgorithmUtils.php");
include_once("../lib/impl/MerchantImpl.php");
include_once("../lib/impl/CouponImpl.php");

$arr = array(23, 22, 41, 18, 20, 12, 200303,2200,1192);

$algorithm = new AlgorithmUtils();

//$arrSort = $algorithm->quick_sort($arr);
//
//var_dump($arrSort);

calFeaturedCoupon();

function calFeaturedCoupon()
{
	$algorithm 		= new AlgorithmUtils();
	$oMerchantImpl 	= new MerchantImpl(DB_TAG_COUPON);
	$oCouponImpl 	= new CouponImpl(DB_TAG_COUPON);
	
	$oMerchants = $oMerchantImpl->getValidAllMerchants();
	
	$tmp = 0;
	foreach($oMerchants as $oMerchant){
		$attributes = $oMerchantImpl->getAttributes($oMerchant->getId());
		
		$oCoupons 	= $oCouponImpl->getCouponsByMerchant($oMerchant->getId(), 0, 10000);
		
		$arrSortCoupons = $algorithm->getFeaturedCoupon($oCoupons);
		
		$merchantWeight = 0;
		if(is_array($attributes) && count($attributes) > 0){
			if(array_key_exists("19", $attributes)){
				$merchantWeight = 0;
			}
		}
		
		//set first top coupon
		$oCouponImpl->setFeaturedByMerchantId($oMerchant->getId());
		if(count($arrSortCoupons) > 0){
			$oTopCoupon = array_pop($arrSortCoupons);
			$oCouponImpl->setFeatured($oTopCoupon->getId(), $oTopCoupon->getRsortValue());
		}
		
		
//		echo "**********************************************************************8<br>";
//		foreach($arrSortCoupons as $key=>$oCoupon){
//			echo "=============================================<br>";
//			echo "coupon id: " . $oCoupon->getId() . "<br>";
//			echo "coupon key: " . $key . "<br>";
//			echo "coupon code: " . $oCoupon->getCode() . "<br>";
//			echo "coupon name: " . $oCoupon->getName() . "<br>";
//			echo "coupon description: " . $oCoupon->getDescript() . "<br>";
//			
//			
//		}
//		
//		$tmp++;
//		
//		if($tmp > 30)
//			break;
	}
}

?>